*******************************************************************************
* > Loop over datasets
*******************************************************************************

foreach dataset in gallup gss {

*******************************************************************************
* Load data
*******************************************************************************

use dataset_`dataset', clear

*******************************************************************************
* Set params based on data set
*******************************************************************************

if "`dataset'" == "gallup" {
	rename conf Y
	local mytitle2 "Confidence (Gallup)"
	local treated_target "police"
}
if "`dataset'" == "gss" {
	rename nat Y
	local mytitle2 "Spending (GSS)"
	local treated_target "crimy"
}

*******************************************************************************
* Make the party differences by opinion object
*******************************************************************************

egen Y_dem = mean(Y) if subgroup == "Democrat", by(target year)
egen Y_rep = mean(Y) if subgroup == "Republican", by(target year)
collapse Y_dem Y_rep, by(target year)
gen Y = Y_dem - Y_rep

*******************************************************************************
* Make lags
*******************************************************************************

su year
local mymin = r(min)

local lags
levelsof year if year < 2014, local(levels)
foreach i of local levels {
	egen Y`i'm = mean(Y) if year == `i', by(target)
	egen Y`i' = mean(Y`i'm), by(target)
	drop Y`i'm
	local lags `lags' Y`i'
}

*******************************************************************************
* Set panel and get the treated ID
*******************************************************************************

encode target, gen(id)
xtset id year
su id if target == "`treated_target'"

*******************************************************************************
* Synth
*******************************************************************************

synth Y `lags', trunit(`r(mean)') trperiod(2014) keep(tmp, replace)
use tmp, clear
erase tmp.dta

*******************************************************************************
* Get table of weights
*******************************************************************************

keep if !mi(_W_)
drop _Y_* _time
rename _W_ Weight_


*******************************************************************************
* Format table
*******************************************************************************

decode _Co_, gen(target)
replace target = subinstr(target, "_", " ", 1)
drop _Co_
order target

if "`dataset'" == "gallup" {
	replace target = proper(target)
	replace target = "Supreme Court" if target == "Sup Ct"
	replace target = "TV News" if target == "Tv News"
}
if "`dataset'" == "gss" {
	replace target = "Foreign aid" if target == "aid"
	replace target = "Foreign aid alt" if target == "aidy"
	replace target = "Military" if target == "arms"
	replace target = "Military alt" if target == "armsy"
	replace target = "Cities" if target == "city"
	replace target = "Cities alt" if target == "cityy"
	replace target = "Crime" if target == "crime"
	replace target = "Drugs" if target == "drug"
	replace target = "Drugs alt" if target == "drugy"
	replace target = "Education" if target == "educ"
	replace target = "Education alt" if target == "educy"
	replace target = "Environment" if target == "envir"
	replace target = "Environment alt" if target == "enviy"
	replace target = "Welfare" if target == "fare"
	replace target = "Welfare alt" if target == "farey"
	replace target = "Health" if target == "heal"
	replace target = "Health alt" if target == "healy"
	replace target = "Transportation" if target == "mass"
	replace target = "Parks and rec" if target == "park"
	replace target = "Blacks" if target == "race"
	replace target = "Blacks alt" if target == "racey"
	replace target = "Roads" if target == "road"
	replace target = "Social Security" if target == "soc"
	replace target = "Space" if target == "spac"
	replace target = "Space alt" if target == "spacy"
}

*******************************************************************************
* Bold non-zero entries
*******************************************************************************

gen any_nonzero = Weight_ > 0
tostring Weight*, replace force format(%9.0g)
foreach x in target Weight_ {
	replace `x' = "\bf{" + `x' + "}" if any_nonzero == 1 & `x' != "0"
	replace `x' = "" if `x' == "0"
}
drop any_nonzero 

*******************************************************************************
* Output table
*******************************************************************************

#delimit;

listtex 
	using "table_sc_partydiffs_`dataset'.tex"
	,
	replace
	type rstyle(tabular)
	head(
		\begin{tabular*}
		{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{2}{l}}
		\toprule 
		Series & Weight
		\\
		\midrule
	)
	foot("\bottomrule\end{tabular*}")
	;
	
#delimit cr

*******************************************************************************
* > Close dataset loop
*******************************************************************************

}

*******************************************************************************
* End
*******************************************************************************
